import React, { PureComponent } from 'react'

export default LoadComponent => (

    class AsyncComponent extends PureComponent {

        constructor() {
            super();
            this.state = {
                Component: null,
                success: true
            }
        }

        destroy = false;

        componentDidMount() {
            this.loadComponent();
        }

        componentWillUnmount() {
            this.destroy = true;
        }

        loadComponent() {
            let Component = null,
                success   = true;
            LoadComponent().then( module => {
                Component = module.default;
            }).catch(() => {
                success = false;
            }).finally(() => {
                if(this.destroy) return;
                this.setState({ Component, success });
            });
        }

        getParams() {
            const { params = {} } =this.props.match;
            return {
                ...this.props,
                params
            }
        }

        render() {
            const { Component } = this.state;
            return (
                    <div >
                        {!!Component ? <Component {...this.getParams()}/> : null}
                    </div>
            )
        }
    }

);






